package mylab.common.context;

import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import mylab.usm.api.model.account.User;


@Slf4j
@UtilityClass
public class UserContext {

    /**
     * 线程变量
     * <p>
     * ## TransmittableThreadLocal  支持多线程传递参数
     * 1、支持父子线程传递变更
     * 2、在使用线程池时，解决线程复用时变量复制的问题
     */
    private final ThreadLocal<User> threadLocal = new TransmittableThreadLocal<>();

    public User getUser() {
        return threadLocal.get();
    }

    public void setUser(User user) {
        threadLocal.set(user);
    }


    public void clear() {
        threadLocal.remove();
    }


}
